package com.tyrone.hephaestus.broker.entity;

import com.sun.org.slf4j.internal.LoggerFactory;
import com.tyrone.hephaestus.broker.register.Registry;
import com.tyrone.hephaestus.common.entity.Msg;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayDeque;
import java.util.Deque;

public class Repository {
    private static Repository repository = new Repository();

    private static Deque<Msg> deque = new ArrayDeque(10);
    
    private static Deque<Msg> deletedDeque = new ArrayDeque(10);


    private Repository() {
    }

    public static Repository getRepository() {
        return repository;
    }

    /**
     * 向仓库内存储
     *
     * @param msg
     */
    public boolean store(Msg msg) {
        boolean added = deque.add(msg);
        if (added) {
            //通知消费者
            Registry.boardCast(msg);
        }
        return added;
    }

    /**
     * 销毁库存
     *
     * @return
     */
    public Msg loss() {
        Msg msg = deque.poll();
        deletedDeque.add(msg);
        return msg;
    }
}
